亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

網(wǎng)頁爬蟲 - Python3.6 下的爬蟲總是重復(fù)爬第一頁的內(nèi)容

瀏覽:118日期:2022-06-30 17:08:03

問題描述

問題如題:改成while,試了很多,然沒有效果,請(qǐng)教大家

# coding:utf-8# from lxml import etreeimport requests,lxml.html,osclass MyError(Exception): def __init__(self, value):self.value = value def __str__(self):return repr(self.value) def get_lawyers_info(url): r = requests.get(url) html = lxml.html.fromstring(r.content) # phones = html.xpath(’//span[@class='law-tel']’) phones = html.xpath(’//span[@class='phone pull-right']’) # names = html.xpath(’//p[@class='fl']/p/a’) names = html.xpath(’//h4[@class='text-center']’) if(len(phones) == len(names)):list(zip(names,phones))phone_infos = [(names[i].text, phones[i].text_content()) for i in range(len(names))] else:error = 'Lawyers amount are not equal to the amount of phone_nums: '+urlraise MyError(error) phone_infos_list = [] for phone_info in phone_infos:if(phone_info[0] == ''): info = '沒留姓名'+': '+phone_info[1]+'rn'else: info = phone_info[0]+': '+phone_info[1]+'rn'print (info)phone_infos_list.append(info) return phone_infos_listdir_path = os.path.abspath(os.path.dirname(__file__))print (dir_path)file_path = os.path.join(dir_path,'lawyers_info.txt')print (file_path)if os.path.exists(file_path): os.remove(file_path)with open('lawyers_info.txt','ab') as file: for i in range(1000):url = 'http://www.xxxx.com/cooperative_merchants?searchText=&industry=100&provinceId=19&cityId=0&areaId=0&page='+str(i+1)# r = requests.get(url)# html = lxml.html.fromstring(r.content)# phones = html.xpath(’//span[@class='phone pull-right']’)# names = html.xpath(’//h4[@class='text-center']’) # if phones or names:info = get_lawyers_info(url)for each in info: file.write(each.encode('gbk'))

問題解答

回答1:

# coding: utf-8import requestsfrom pyquery import PyQuery as Qurl = ’http://www.51myd.com/cooperative_merchants?industry=100&provinceId=19&cityId=0&areaId=0&page=’with open(’lawyers_info.txt’, ’ab’) as f: for i in range(1, 5):r = requests.get(’{}{}’.format(url, i))usernames = Q(r.text).find(’.username’).text().split()phones = Q(r.text).find(’.phone’).text().split()print zip(usernames, phones)

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 日本二本三本二区 | 亚洲最大网站在线 | 国产乱码亚洲精品一区二区 | 欧美日韩成人午夜免费 | 亚洲欧美国产一区二区三区 | 国产精品综合色区在线观看 | 精品中文字幕在线观看 | 九九热精品在线观看 | 国产黄a三级三级三级 | 国产精品1024在线永久免费 | 伊人中文在线 | 手机在线黄色网址 | 日韩精品福利视频一区二区三区 | 青草国产精品久久久久久 | 岛国片欧美一级毛片 | 99久久国产综合精品女不卡 | 欧美伦理三级在线播放影院 | 免费成人福利视频 | 日韩亚洲一区中文字幕 | 日日噜噜夜夜狠狠视频无 | 好爽~好硬~好紧~蜜芽 | 朴妮唛19禁福利视频在线 | 欧美系列在线播放 | 99re热久久 | 国产成人午夜福在线观看 | 国产一国产一有一级毛片 | 毛片黄| 国产精品爱久久久久久久三级 | 久草视频一区 | 在线观看黄色片网站 | 91久久精品国产免费一区 | 欧美日韩视频在线一区二区 | 亚洲国产情侣偷自在线二页 | 国内精品久久久久鸭 | 亚洲一级毛片中文字幕 | 性色影院| 欧美一区二区亚洲 | 欧美一区二区三区国产精品 | 看片视频在线观看 | 国产成人精品999在线 | 军营呻吟娇喘np |