本次学习的教学视频来自嵩天老师的网络爬虫教学,主要学习内容有requests\BeautifulSoup\scrapy\re,目前除了scrapy其他刚好看完。并搬运实现了一些小项目如58同城租房信息爬取、淘宝搜索商品项目,现将从爬虫基本方法、实战和遇到的问题三个方面进行总结。 1.基本方法 首先就是requests库,是python最简易实用的HTTP库,是一个请求库。主要方法如下,其中requests.request()方法最常用,用于构造请求,是其他几种方法的总和。其余方法如get()获取HTML网页,head()获取网页head标签,post()\pu()t用于提交对应请求,patch()进行局部修改,delete()提交删除请求。 着重介绍request.get()方法,requests.get(url, params=None,**kwargs) 其中url为页面链接,params为额外参数,字典格式,**kwargs包含了12个控制访问的参数。(params\data\json\headers\cookies\auth\files\timeout\proxies\allow_redirects\stream\verify\cert) 通常我们使用get()方法获取页面的内容。 接着介绍请求得到的Response对象,见下表。 补充几段常用代码。 (1)爬取京东商品 import requestsurl = 'https://item.jd.com/2967929.html'try: r = requests.get(url) r.raise_for_status() #如果发送了错误请求,可以抛出异常 r.encoding = r.apparent_encoding #把文本内容的编码格式传递给头文件编码格式 print(r.text[:1000])except: print('爬取失败!') (2)爬取亚马逊,需要修改headers字段,模拟请求
(3)百度搜索关键词提交-params提交关键词 import requestsurl='http://www.baidu.com/s'try: kv={'wd':'Python'} r=requests.get(url,params=kv) print(r.request.url) r.raise_for_status() print(len(r.text)) print(r.text[500:5000])except: print('爬取失败') (4)图片爬取存储
下面介绍BeautifulSoup库,用于对网页内容进行解析。 BeautifulSoup(mk, 'html.parser'),可以用html.parser\lxml\xml\html5lib作为解析器,这里选取html.parser。 元素主要有Tag\Name\Attributes\NavigableString\Comment。其中Tag使用方法如(soup.a),attrs使用如(a.attrs['class']),Navigable(tag.string)为非属性字符串,comment即注释。 标签树的遍历方法有(上行遍历、下行遍历、平行遍历) 此外可以用soup.prettify()输出有层次感的段落。 信息提取方法如下:常用find_all,具体对标签搜索有soup.find_all('a'),对属性搜索有soup.find_all('p',class='course'),对字符串搜索有soup.find_all(string='...'),配合正则表达式检索有soup.find_all(re.compile('link'))。 find() 搜索且返回一个结果,字符串类型 find_parents() 在先辈节点中搜索,返回一个列表类型 find_parent() 在先辈节点中返回一个结果,字符串类型 find_next_siblings() 在后续平行节点搜索,返回列表类型 find_next_sibling() find_previous_siblings() find_previous_sibling() 在前序平行节点中返回一个结果,字符串类型 find_all(name,attrs,recursive,string,**kwargs) 返回一个列表类型,存储查找的结果 参数: name:对标签名称的检索字符串,可以使用列表查找多个标签,find_all(true)所有标签 attrs:对标签属性值的检索字符串,可标注属性检索 例如find_all('a','href') recursive:是否对子孙所有节点搜索,默认值为true,false则值查找当前节点儿子的信息 string:<></>中字符串区域的检索字符串 最后介绍Re正则表达式库。 正则表达式限定符如下: 贪婪匹配指匹配的数据无限多,所谓的的非贪婪指的是匹配的次数有限多。一般情况下,非贪婪只要匹配1次。*、+限定符都是贪婪的,因为它们会尽可能多的匹配文字,只有在它们的后面加上一个?就可以实现非贪婪或最小匹配。 在re库中一般使用raw string类型即r'text'。其中遇到特殊字符需要 \ 加以转义。 方法如下
flag = 0中有三种选择类型,re.I忽略大小写、re.M从每行开始匹配、re.S匹配所有字符。 以上是函数式用法,此外还有面向对象用法。 pat = re.compile('')pat.search(text) 最后介绍match对象的属性和方法,见下。
2.实战演练 主要选取了淘宝商品搜索和58同城租房两个实例,链接分别为‘https://blog.csdn.net/u014135206/article/details/103216129?depth_1-utm_source=distribute.pc_relevant_right.none-task-blog-BlogCommendFromBaidu-8&utm_source=distribute.pc_relevant_right.none-task-blog-BlogCommendFromBaidu-8‘ 'https://cloud.tencent.com/developer/article/1611414' 淘宝搜索import requestsimport redef getHTMLText(url): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0' } #cookies在元素审查,网络里面刷新,找请求头下面的Cookie usercookies = '' #这里需要使用客户端的淘宝登录cookies cookies = {} for a in usercookies.split(';'): name, value = a.strip().split('=', 1) cookies[name] = value print(cookies) try: r = requests.get(url, headers=headers, cookies=cookies, timeout=60) r.raise_for_status() #如果有错误返回异常 print(r.status_code) #打印状态码 r.encoding = r.apparent_encoding return r.text except: return 'failed'def parsePage(ilt, html): try: plt = re.findall(r'\'view_price\'\:\'[\d\.]*\'', html) tlt = re.findall(r'\'raw_title\'\:\'.*?\'', html) for i in range(len(plt)): price = eval(plt[i].split(':')[1]) # 意义是进行分割其冒号 title = eval(tlt[i].split(':')[1]) ilt.append([price, title]) except: print('')def printGoodsList(ilt): tplt = '{:4}\t{:8}\t{:16}' print(tplt.format('序号', '价格', '商品名称')) # 输出信息 count = 0 for g in ilt: count = count + 1 print(tplt.format(count, g[0], g[1]))def main(): goods = '足球' depth = 3 start_url = 'http://s.taobao.com/search?q={}&s='.format(goods) # 找到起始页的url链接 infoList = [] for i in range(depth): # 进行循环爬去每一页 try: url = start_url + str(44 * i) html = getHTMLText(url) parsePage(infoList, html) except: continue printGoodsList(infoList)main() 58同城爬取租房,这部分代码较多,选取重要内容展示。 1.加密字体的解码
2.租房信息爬取 # 提取租房信息def parse_pages(pages): num = 0 soup = BeautifulSoup(pages, 'lxml') # 查找到包含所有租房的li标签 all_house = soup.find_all('li', class_='house-cell') for house in all_house: # 标题 # title = house.find('a', class_='strongbox').text.strip() # print(title) # 价格 price = house.find('div', class_='money').text.strip() price = str(price) print(price) # 户型和面积 layout = house.find('p', class_='room').text.replace(' ', '') layout = str(layout) print(layout) # 楼盘和地址 address = house.find('p', class_='infor').text.replace(' ', '').replace('\n', '') address = str(address) print(address) num += 1 print('第' + str(num) + '条数据爬取完毕,暂停3秒!') time.sleep(3) with open('58.txt', 'a+', encoding='utf-8') as f: #这里需encoding编码为utf-8,因网络读取的文本和写入的文本编码格式不一;a+继续在文本底部追加内容。 f.write(price + '\t' + layout + '\t' + address + '\n') 3.由于58会封禁爬虫IP地址,还需要爬取ip进行切换。
3.经验总结 期间遇到问题汇总如下: 1.大多数网站都需要模拟请求头,user-agent。 2.淘宝需要模拟cookies登陆,cookies信息可以在检查元素中找到。 3.这种方法只能爬取静态的网页,对于数据写入javascript的动态网页,还需要新的知识。 4.爬取过程中容易被封IP,需要在IP代理网站爬取IP地址,不断刷新IP地址。在get()方法中增加proxies参数即可。 5.58的价格字符串采用的加密的方式显示,需要解码。 6.写入文本时要用encoding='utf-8'编码,避免出错。 |
|