首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Webscrape w/o美汤

Webscrape w/o美汤
EN

Stack Overflow用户
提问于 2016-12-03 10:56:19
回答 1查看 61关注 0票数 0

一般来说,我对web scraping和python是个新手,但是我有点纠结于如何纠正我的函数。我的任务是抓取以特定字母开头的单词的站点,并返回匹配的单词列表,最好使用正则表达式。感谢您的时间,这是我到目前为止的代码。

代码语言:javascript
运行
复制
import urllib
import re

def webscraping(website):
    fhand = urllib.urlopen(website).read()
    for line in fhand:
        line = fhand.strip()
        if line.startswith('h'):
            print line
webscraping("https://en.wikipedia.org/wiki/Web_scraping")
EN

回答 1

Stack Overflow用户

发布于 2016-12-03 14:02:32

不要使用正则表达式来解析HTML,您可以使用Beautiful Soup

代码语言:javascript
运行
复制
import urllib
from BeautifulSoup import *

todo = list()
visited = list()
url = raw_input('Enter - ')
todo.append(url)

while len(todo) > 0 :
   print "====== Todo list count is ",len(todo)
   url = todo.pop()

   if ( not url.startswith('http') ) : 
       print "Skipping", url
       continue

   if ( url.find('facebook') > 0 ) :
       continue

   if ( url in visited ) :
       print "Visited", url
       continue

   print "===== Retrieving ", url

   html = urllib.urlopen(url).read()
   soup = BeautifulSoup(html)
   visited.append(url)

   # Retrieve all of the anchor tags
   tags = soup('a')
   for tag in tags:
       newurl = tag.get('href', None)
       if ( newurl != None ) : 
           todo.append(newurl)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40943789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档