代码:
topic_url = parse.urljoin(domain, tr.xpath(".//td[3]/a/@href").extract()[-1])
topic_title = tr.xpath(".//td[3]/a/text()").extract()[-1]
报错:
/Users/shihaotian/Desktop/venv/lib/python3.7/site-packages/pymysql/cursors.py:170: Warning: (1364, "Field 'author_id' doesn't have a default value")
result = self._query(query)
Traceback (most recent call last):
File "/Users/shihaotian/PycharmProjects/untitled/spider.py", line 126, in <module>
parse_list(url)
File "/Users/shihaotian/PycharmProjects/untitled/spider.py", line 81, in parse_list
topic_url = parse.urljoin(domain, tr.xpath(".//td[3]/a/@href").extract()[-1])
IndexError: list index out of range
上面取[-1]是因为csdn在第一二条中新增了推荐a标签,所以取最后一个能正常取得数据