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

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

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

瀏覽:138日期: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 編程
相關文章:
主站蜘蛛池模板: 黄页网址大全免费观看不用 | 婷婷国产天堂久久综合五月 | 一级黄色片免费观看 | 免费一级真人毛片 | 久久999精品| 国产色一区| 日本成人网址 | 久久91视频 | 国产欧美精品一区二区色综合 | 激情在线日韩视频免费 | 亚洲国产美女在线观看 | 亚洲欧美不卡 | 国产视频精品免费 | 亚洲视频免费在线播放 | 亚洲成人精品 | 97视频在线免费观看 | 免费国产成人高清在线观看视频 | 成人免费视频在 | 久久人体做爰大胆图片 | 同性男男黄h片在线播放免费 | 久99久视频 | 亚洲免费区 | 日本黄色www | 超级乱淫片67194免费看 | 久久福利国产 | 王色在线观看视频 | 亚洲欧美成人一区二区在线电影 | 国产成人一区 | 日本va视频 | 亚洲性大片 | 手机在线国产视频 | 日本二级黄色片 | 在线一区 | 亚洲欧美日韩在线精品一区二区 | 欧美日韩一区二区三区在线 | 青青草a国产免费观看 | 日本中文字幕不卡在线一区二区 | 最新亚洲人成网站在线影院 | 国产免费久久精品久久久 | 玖玖玖视频在线观看视频6 玖玖视频精品 | 猫咪视频成人永久免费观看 |