What is the error reported? Generally, it is correct to copy the xpath directly to the web page.
Ask me, I run this error, what is the reason for searching for a long time did not find the reason
hl = etree.HTML(html)
for i in range(30):
txt = hl.xpath(f'//*[@id="contentf"]/div[18]/div[{i}]//text()')
url = hl.xpath(f'//*[@id="content"]/div[18]/div[{i}]//@href')
a = list(set(txt))
if a:
out = [x.strip() for x in a if x.strip() !='']
# print(out)
if url:
out.append(*url)
print(out)
#报错提示
Traceback (most recent call last):
File "/Users/leslie/Desktop/b/10_代码演练/xx_20爬虫lx.py", line 17, in <module>
out.append(*url)
NameError: name 'out' is not defined. Did you mean: 'oct'?
Process finished with exit code 1
0 Answer
What is the error reported? Generally, it is correct to copy the xpath directly to the web page.
If the value of a in line 8 is empty list, the judgment in line 9 does not go in and line 10 does not run. In this case, if line 12 is judged to be true, line 14 out is an undefined variable.
can add initialization before line 8:
a = list(set(txt))
out = []
if a:
out = [x.strip() for x in a if x.strip() !='']
not an error, but no content output is why
url = Hl. Xpath(f '/ / * [the < a > @ id = "content"] / div [18] / div [{I}] / / @ href') the written / @ href single diagonal < / a >
这家伙很懒,什么都没留下...