|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 暗夜之隐 于 2018-10-8 14:33 编辑
问题一:本想做一个淘宝商品信息抓取的爬虫,发现每次抓取的都并非这一页的所有内容。最后2排的商品在源码里面都找不到 那它是如何显示或是用了什么方法隐藏吗
问题二:有时会出现错误,但不是每次 关了重开又可以了
注意没有登录账号 所有有时会返回0 是跳转到登录页面了并且登录页面还是一个gbk的
代码如下
- UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 99: invalid continuation byte
复制代码
- import urllib.request
- from urllib.request import quote
- import re
- def urlopen(url):
-
- req = urllib.request.Request(url)
- req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36")
- html = urllib.request.urlopen(req)
- html = html.read()
- return html
- def content():
-
- word = '摄像头'
- word = quote(word)
- url = 'https://s.taobao.com/search?initiative_id=tbindexz_20170306&ie=utf8&spm=a21bo.2017.201856-taobao-item.2&sourceId=tb.index&search_type=item&ssid=s5-e&commend=all&imgfile=&q={}&suggest=history_5&_input_charset=utf-8&wq=&suggest_query=&source=suggest'.format(word)
- print(url)
- cc = urlopen(url)
- con = cc.decode("utf-8")
- bian = re.compile(r'\xa9|\u2b50')
- con = re.sub(bian,'',con)
- with open("taobao.txt",'w',)as f:
-
- f.write(con)
- return con
- con = content()
-
- aa = re.findall(r'"nick":".+?","shopcard"',con)
- print(len(aa))
- #这里输放的是店铺名字的数量 只有36个 但应该是有48个才对 一共12排 一排4个
- # 另外边上的一排也没有广告位置的信息也没有呢
复制代码
本帖最后由 wongyusing 于 2018-10-9 11:05 编辑
看代码,注意,填写饼干的信息
- import requests
- import re
- import json
- import pandas
- # 打开网页函数
- def get_response(url,commodity):
- headers = {
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
- 'Accept-Encoding': 'gzip, deflate, br',
- 'Accept-Language': 'zh-CN,zh;q=0.9',
- 'Cache-Control': 'no-cache',
- 'Connection': 'keep-alive',
- 'Pragma': 'no-cache',
- 'Upgrade-Insecure-Requests': '1',
- 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
- 'cookie': 'xxxx',# cookie就不提供了,注意里面的 isg 参数 (需要你点击搜索栏后才会显示真正的参数)
- }
- params = {
- 'q': commodity,
- 'imgfile': {
- 'js': '1',
- },
- 'stats_click': {
- 'search_radio_all':'1',
- 'initiative_id': 'staobaoz_20181009',
- 'ie': 'utf8',
- }
- }
- response = requests.get(url, headers=headers,params=params) # 加上浏览器头,以防被禁
- response.encoding = 'utf-8' # 指定编码格式
- #response.encoding = 'gbk' # 指定编码格式
- return response
- #OPlCFG6GXQsCAdoP6nmnEljY
- def main():
- # 搜索的商品名
- commodity = 'python书籍'
- base_url = f'https://s.taobao.com/search?q={{ commodity }}&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20181009&ie=utf8'
- response = get_response(base_url,commodity)
-
- req = 'g_page_config = (.*?)g_srp_loadCss'
- items_list = re.findall(req,response.text,re.S)[0].strip()
- js = json.loads(items_list[:-1])
- jd = js['mods']['itemlist']['data']['auctions'] #.keys())
- #
- df = pandas.DataFrame(jd)
- #
- df[['category','raw_title','view_price','item_loc','view_sales']].to_html(f'{commodity}.html')
- if __name__ == "__main__":
- main()
复制代码
这份代码和之前我写的不同点在于,饼干的参数(发英语会被审核)
通过开发者工具可以发现,饼干的 isg参数是淘宝的反爬的主要手段。
当isg参数不被改变,我们就获取不到真正的数据。
触发isg参数改变的开关在于我们是否点击了 搜索栏
我的这份代码中 commodity好像是要 中英结合,直接搜中文的内容,例如:火鸡面。好像是不行的。
找不到原因。
爬取下来的数据在当前的目录下的html文件。
|
|