def getHtml(url,timeout=20):
try:
headers = {
'Accept-Language': 'zh-cn',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Mozilla/4.0 (compatible MSIE 6.00 Windows NT 5.1 SV1)',
}
r = requests.get(url,headers=headers,timeout=timeout)
html = r.text
return html
except Exception,ex:
return None
soup = BeautifulSoup(getHtml())
print soup.title
以上代码,如何改进,才能在获取任何网页标题的时候,不至于乱码。
注:提取部分网页的标题的时候会直接乱码显示。如何改进,才能通用?
绝地无双
慕哥9229398
元芳怎么了
料青山看我应如是
相关分类