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

您的位置:首頁技術文章
文章詳情頁

網頁爬蟲 - Python3.6 下的爬蟲總是重復爬第一頁的內容

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

問題描述

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

# 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)

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 在线视频国产网址你懂的在线视频 | 日本狠狠干 | 久久久久国产 | 美女制服一二三区 | 最新国产成人盗摄精品视频 | 精品推荐 国产 | 中文字幕亚洲精品 | 伊人高清视频 | 全免费一级毛片在线播放 | 久久免费99精品国产自在现线 | 欧美日韩中文一区二区三区 | 国产主播一区二区三区 | 超污视频免费看 | 黄色毛片小视频 | 精品国产香蕉伊思人在线 | 国产精品久久久久久影视 | 日韩中文字幕网 | 免费簧片在线观看 | 欧美一区二区亚洲 | 久久久久久久91精品免费观看 | 亚洲欧美日韩国产综合 | 国精品在亚洲_欧美 | 每日更新国产 | 一区二区在线不卡 | 人妖与黑人做爰 | 国产一区二区不卡视频 | 欧亚毛片| 午夜男人女人爽爽爽视频 | 亚洲丶国产丶欧美一区二区三区 | 欧美三级在线播放 | 欧美高清另类 | 国产成人精品天堂 | 国产精品国产三级国产专播下 | 国产精品成人免费福利 | 国产亚洲欧美成人久久片 | 免费人成激情视频在线看 | 久久综合亚洲 | 国产一级二级三级在线观看 | 午夜视频在线观看一区 | 成人免费观看国产高清 | 亚洲成综合 |