|
发表于 2018-9-10 12:04:17
|
显示全部楼层
- import requests
- import re
- import json
- import pandas
- # 打开网页函数
- def get_response(url):
- headers = {
- 'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36"}
- response = requests.get(url, headers) # 加上浏览器头,以防被禁
- response.encoding = 'utf-8' # 指定编码格式
- #response.encoding = 'gbk' # 指定编码格式
- return response
- def main():
- base_url = 'https://s.taobao.com/search?q=python书籍'
- response = get_response(base_url)
- 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('test_data.html')
- # 你这个问题结合这个文件来说吧
- # 当你运行这个文件时,只会执行在if __name__ == "__main__":下面的main()函数
- # 当这个文件被作为模块调用时,则不会执行下面的main()函数
- if __name__ == "__main__":
- main()
复制代码 |
|