练习中,我一直在用BeautifulSoup学习Python和网页抓取。我正在寻找一个可以在网站上找到团队页面的程序,并且可以删除团队成员的姓名。以下是“团队”页面的示例:http://plasticbank.org/team-speakers/使用BeautifulSoup查找“团队”页面
我已经认识到,所有团队页面都有“团队”显着更大,但并非所有网站都使用标题,因此很难通过它们进行解析。我已经得到尽可能加载与urllib2的URL。我如何浏览一个网站的主页并找到一个“团队”或真正的任何具有特定主题的页面?这与寻找联系页面的问题是一样的,你如何告诉刮板找到它?
这里是我的代码完成的部分:(这只是用来加载网站)
#Pre: url is a string containing the address of a website
#return: A string with the URL formatted to include http://
def ensureurl(url):
if '//' not in url:
return "http://" + url
else:
return url
#Pre: url is a string containing the address of a website
#return: The HTML code at that URL or an empty string if the URL could not be processed
def read_url(url):
url = ensureurl(url)
print url
try:
#User agent spoofing to trick sites into thinking the bot is a human.
#This does not work on all sites.
hdr = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
request = urllib2.Request(url, headers=hdr)
return urllib2.urlopen(request).read()
except urllib2.HTTPError, e:
print e.fp.read()
return ""
你写的任何代码? – elena
我刚刚编辑帖子以包含我的代码的完整部分 –