请问这段代码有什么错误呀,它不会创建文件也不会返回任何东西
import requestsfrom pyquery import PyQuery as pq
try:
url = 'https://www.zhihu.com/explore'
headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
html = requests.get(url,headers=headers).text
doc = pq(html)
items = doc('.explore-tab .feed-item').items()
for item in items:
question = item.find('h2').text()
auther = item.find('.auther-link-line').text()
answer = pq(item.find('.content').html()).text()
print(question)
print(auther)
print(answer)
with open('explore.txt','a',encoding='utf-8') as f:
f.write('\n'.join())
f.write('\n' + '=' *50 + '\n')
except:
print('异常')
如题,这是跟着一些书写的
页:
[1]