在python爬虫中使用for循环填入网页id并嵌套函数爬取信息时,无法正确爬取

使用了三个网页ID:38635、38636、38627来测试,输出dic的结果为三个同样的字典(都对应38635),

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
url='https://db.yaozh.com/hmap?grade=%E5%85%A8%E9%83%A8&p={}&pageSize=30&province=%E5%B9%BF%E4%B8%9C%E7%9C%81&type=%E5%85%A8%E9%83%A8'
info_url_model='https://db.yaozh.com{}'


for x in ['/hmap/38625.html', '/hmap/38626.html', '/hmap/38627.html']:  #test
        info_url=info_url_model.format(x)      #填入医院urlID    
        detail = requests.get(info_url, headers=headers)  # 取得内容页面内容
        detail.encodint = 'utf-8'
        soup2 = BeautifulSoup(detail.text, 'html.parser')  
        a = soup2.select('.toFindImg')
        for b in soup2.find_all(lambda tag: tag.name == 'span' and tag.get('class') == ['toFindImg']):
            item_list.append(b.text)
            for i in range(len(item_list)):                           #去掉空格
                item_list[i] = item_list[i].replace(' ', '')          #去掉空格
                item_list[i] = item_list[i].replace('\n', '')       #去掉换行符
            dic = dict(zip(item_name, item_list))  # 生成字典
        print(dic)

可以试试java的爬虫框架gecco

你试一下手动填你列表中那些网址的时候是能正确打开网页吗

安全限制,把java沙箱中添加安全地址试试

你把format之后的url打印出来看看,估计就能发现问题了,我实习也是在做爬虫的,欢迎互帮互助

//循环到这里看一下soup2的url是不是你想要的
 soup2 = BeautifulSoup(detail.text, 'html.parser')  

然后
你的for b in ...循环每次的最后都会重写dic的值 不奇怪么?还是写错了?
最后可以直接抓table中的所有tr 循环获取信息就行了....
有点乱 凑合看吧