循环提取tr的时候会重复提取第一个

来源:14-10 获取和解析列表页-1

JethroDD

2020-05-18

def parse_list(url):
    resp_text = requests.get(url,cookies= cookies_dict).text
    sel = Selector(text=resp_text)
    all_trs = sel.xpath('//table[@class="forums_tab_table"]//tbody//tr')
    for tr in all_trs:
        # print(tr)
        status = tr.xpath('//td[1]/span/text()').get()
        score = tr.xpath('//td[2]/em/text()').get()
        topic_test = tr.xpath('//td[3]/span[@class="green"]')
        if topic_test:
            topic_url = parse.urljoin(domain,tr.xpath('//td[3]/a[2]/@href').get())
            topic_title =tr.xpath('//td[3]/a[2]/text()').get()
        else:
            topic_url = parse.urljoin(domain,tr.xpath('//td[3]/a/@href').get())
            topic_title =tr.xpath('//td[3]/a[1]/text()').get()
        author_url=tr.xpath('//td[4]/a/@href').get()
        author_id = author_url.split('/')[-1]
        creat_time = tr.xpath('//td[4]/em/text()').get()
        answer_info =tr.xpath('//td[5]/span/text()').get()
        answer_nums = answer_info.split('/')[0]
        click_nums = answer_info.split('/')[1]
        last_time_str =tr.xpath('//td[6]/em/text()').get()
        last_time = datetime.strptime(last_time_str,'%Y-%m-%d %H:%M')

传入一个url来测试的时候只会提取该url下的第一篇文章的,并且后面提取出来的值都是同一个
请问这是咋回事

写回答

1回答

bobby

2020-05-19

如果xpath提取有问题 你可以尝试将这个地方的逻辑改成css选择器试试

0
0

Python爬虫工程师实战 大数据时代必备

慕课网严选精品教程,高质量内容+服务!

2377 学习 · 1158 问题

查看课程