html - 为什么BeautifulSoup find_all 返回的list都不是按照网页显示顺序排序的?
高洛峰
高洛峰 2017-04-18 10:26:11
0
1
581

我想爬糗百的段子,显示作者,和对应的段子,先只爬第一面

import requests
from bs4 import BeautifulSoup as bs
page = 1
url = "http://www.qiushibaike.com/hot/page/"
r = requests.get(url+str(page))
soup = bs(r.content,"html.parser")
names = []
for n in  soup.find_all("a",{"href":True,"target":True,"title":True}):
    names += [n.h2.get_text()]
print(names)

print 结果:

['威信红包接龙关注', 'ZBZBZBZ', '养贼专业户', '柱流年', '大风起兮雪飞扬', '依然家的麦芽糖', '小小&妖精', '苍南下山耍流氓,黑衣格哥买红糖', '九妹妹~', '亲爱de橄榄绿', '有点胖的妮妮', '污奇奇', '没办法就是这么帅', '亦龙', '哇噻~桃宝', '单名一个饭字', 'ni敢拿命疼我吗?', '许我三日暖', '半阙词曲丶', '谁动了我的粮食?']

html.fromstring xpath 也这样

from lxml import html
import requests
page = 1
url = "http://www.qiushibaike.com/hot/page/"
r = requests.get(url+str(page))
tree = html.fromstring(r.content)
print(tree.xpath('//*[@class="article block untagged mb15"]/p[1]/a[2]/h2/text()'))

print 结果:

['威信红包接龙关注', 'ZBZBZBZ', '养贼专业户', '大风起兮雪飞扬', '柱流年', '依然家的麦芽糖', '小小&妖精', '苍南下山耍流氓,黑衣格哥买红糖', '九妹妹~', '亲爱de橄榄绿', '有点胖的妮妮', '污奇奇', '没办法就是这么帅', '亦龙', '单名一个饭字', 'ni敢拿命疼我吗?', '许我三日暖', '半阙词曲丶', '根@儿', '我是你的绝无仅有']

但网页的实际显示顺序是:

['威信红包接龙关注', '养贼专业户', 'ZBZBZBZ', '柱流年', '有点胖的妮妮', '依然家的麦芽糖', '小小&妖精', '大风起兮雪飞扬', '苍南下山耍流氓,黑衣格哥买红糖', '九妹妹~',  '亦龙'...]

如何让返回的list的元素的顺序和网页显示顺序相同?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
大家讲道理

It should be because the sorting of the content on the page is changing. His ranking is based on the "funny" number in the comments. The funny numbers are close, the order changes normally, and sometimes new jokes are added to this page. The time when your browser retrieves the page is different from the time when the crawler crawls it. It is normal that the order of the paragraphs you see is different.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!