比較常用
# -*-coding:utf8-*-import requestsfrom lxml import etreeurl="http://econpy.pythonanywhere.com/ex/001.html"page=requests.get(url)html=page.textselector = etree.HTML(html)buyer=selector.xpath('//div[@title="buyer-name"]/text()')這個用的少一些# -*-coding:utf8-*-import requestsfrom lxml import htmlurl="http://econpy.pythonanywhere.com/ex/001.html"page=requests.get(url)tree=html.fromstring(page.text)buyer=tree.xpath('//div[@title="buyer-name"]/text()')prices=tree.xpath('//span[@class="item-price"]/text()')print (buyer)print (prices)Xpath的語法參考 http://www.companysz.com.cn/xpath/xpath_syntax.aspChrome中使用時可以下載插件:Xpath helper參考使用requests和lxml編寫python爬蟲小記 http://www.tuicool.com/articles/vABNRbRXPath在python中的高級應用 參見:http://blog.csdn.net/winterto1990/article/details/47903653
但是遇到中文網頁時,中文出現亂碼。
req = requests.get("http://news.sina.com.cn/")print (req.text)為了解決這個問題,請參考這篇文章: http://blog.csdn.net/chaowanghn/article/details/54889835
新聞熱點
疑難解答