为什么会出错???
程序代码:
import re import requests as r import pymongo as pmg client = pmg.MongoClient('localhost', 27017) mydb = client['mydb'] b = mydb['编程论坛-python'] g = b.insert_one headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit\ /537.36(KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36' } def get_urls_and_titles(url): html = r.get(url, headers=headers).text urls = re.findall('<a href="(.*?)" title="发帖时间:', html) titles = re.findall('<a href="thread-.*?">(.*?)</a>', html) urls = sets(urls) for i in range(4): del titles[1] titles = tuple(titles) write((titles, urls, ('title', 'url'))) def sets(things): a = set(things) a = tuple(a) return a def write(*args): for each in args: for infos1, infos2, info3 in each[0], each[1], each[2]: for info1, info2 in infos1, infos2: info = { info3[0]: info1, info3[1]: info2 } g(info) if __name__ == '__main__': get_urls_and_titles('http://bbs.bccn.net/forum-246-1.html')
完了就报错:
Traceback (most recent call last):
File "E:/360Downloads/周煜/Python/程序/b.py", line 38, in <module>
get_urls_and_titles('http://bbs.bccn.net/forum-246-1.html')
File "E:/360Downloads/周煜/Python/程序/b.py", line 22, in get_urls_and_titles
write((titles, urls, ('title', 'url')))
File "E:/360Downloads/周煜/Python/程序/b.py", line 29, in write
for infos1, infos2, info3 in each[0], each[1], each[2]:
ValueError: too many values to unpack (expected 3)