四种Python爬虫常用的定位元素方法对比,你偏爱哪一款?( 二 )

3. XPathXPath 即为 XML 路径语言,它是一种用来确定 XML 文档中某部分位置的计算机语言,如果使用 Chrome 浏览器建议安装 XPath Helper 插件,会大大提高写 XPath 的效率 。
之前的爬虫文章基本都是基于 XPath,大家相对比较熟悉因此代码直接给出:
import requestsfrom lxml import htmlurl = 'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-1'response = requests.get(url).textdef xpath_for_parse(response):    selector = html.fromstring(response)    books = selector.xpath("//ul[@class='bang_list clearfix bang_list_mode']/li")    for book in books:        title = book.xpath('div[@class="name"]/a/@title')[0]        print(title)if __name__ == '__main__':    xpath_for_parse(response)4. 正则表达式如果对 HTML 语言不熟悉,那么之前的几种解析方法都会比较吃力 。这里也提供一种万能解析大法:正则表达式,只需要关注文本本身有什么特殊构造文法,即可用特定规则获取相应内容 。依赖的模块是 re
首先重新观察直接返回的内容中,需要的文字前后有什么特殊:
import requestsimport reurl = 'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-1'response = requests.get(url).textprint(response)

四种Python爬虫常用的定位元素方法对比,你偏爱哪一款?

文章插图
 

四种Python爬虫常用的定位元素方法对比,你偏爱哪一款?

文章插图
 
观察几个数目相信就有答案了:<div class="name"><a href=https://www.isolves.com/it/cxkf/yy/Python/2021-03-04/"http://product.dangdang.com/xxxxxxxx.html" target="_blank" title="xxxxxxx"> 书名就藏在上面的字符串中,蕴含的网址链接中末尾的数字会随着书名而改变 。
分析到这里正则表达式就可以写出来了:
import requestsimport reurl = 'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-1'response = requests.get(url).textdef re_for_parse(response):    reg = '<div class="name"><a href=https://www.isolves.com/it/cxkf/yy/Python/2021-03-04/"http://product.dangdang.com/d+.html" target="_blank" title="(.*?)">' for title in re.findall(reg, response): print(title)if __name__ == '__main__': re_for_parse(response)可以发现正则写法是最简单的,但是需要对于正则规则非常熟练 。所谓正则大法好!
当然,不论哪种方法都有它所适用的场景,在真实操作中我们也需要在分析网页结构来判断如何高效的定位元素,最后附上本文介绍的四种方法的完整代码,大家可以自行操作一下来加深体会
import requestsfrom bs4 import BeautifulSoupfrom lxml import htmlimport reurl = 'http://bang.dangdang.com/books/bestsellers/01.00.00.00.00.00-24hours-0-0-1-1'response = requests.get(url).textdef bs_for_parse(response):    soup = BeautifulSoup(response, "lxml")    li_list = soup.find('ul', class_='bang_list clearfix bang_list_mode').find_all('li')    for li in li_list:        title = li.find('div', class_='name').find('a')['title']        print(title)def css_for_parse(response):    soup = BeautifulSoup(response, "lxml")    li_list = soup.select('ul.bang_list.clearfix.bang_list_mode > li')    for li in li_list:        title = li.select('div.name > a')[0]['title']        print(title)def xpath_for_parse(response):    selector = html.fromstring(response)    books = selector.xpath("//ul[@class='bang_list clearfix bang_list_mode']/li")    for book in books:        title = book.xpath('div[@class="name"]/a/@title')[0]        print(title)def re_for_parse(response):    reg = '<div class="name"><a href=https://www.isolves.com/it/cxkf/yy/Python/2021-03-04/"http://product.dangdang.com/d+.html" target="_blank" title="(.*?)">' for title in re.findall(reg, response): print(title)if __name__ == '__main__': # bs_for_parse(response) # css_for_parse(response) # xpath_for_parse(response) re_for_parse(response)


推荐阅读