python爬取京东商品价格教科书中文版_python抓取京东价格分析京东商品价格走势...

代码如下:

from creepy import Crawler

from BeautifulSoup import BeautifulSoup

import urllib2

import json

class MyCrawler(Crawler):

def process_document(self, doc):

if doc.status == 200:

print '[%d] %s' % (doc.status, doc.url)

try:

soup = BeautifulSoup(doc.text.decode('gb18030').encode('utf-8'))

except Exception as e:

print e

soup = BeautifulSoup(doc.text)

print soup.find(id="product-intro").div.h1.text

url_id=urllib2.unquote(doc.url).decode('utf8').split('/')[-1].split('.')[0]

f = urllib2.urlopen('http://p.3.cn/prices/get?skuid=J_'+url_id,timeout=5)

price=json.loads(f.read())

f.close()

print price[0]['p']

else:

pass

crawler = MyCrawler()

crawler.set_follow_mode(Crawler.F_SAME_HOST)

crawler.set_concurrency_level(16)

crawler.add_url_filter('\.(jpg|jpeg|gif|png|js|css|swf)$')

crawler.crawl('http://item.jd.com/982040.html')

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Python爬取京东商品价格,可以按照以下步骤进行操作: 1. 首先,你需要安装`beautifulsoup4`和`requests`这两个库。你可以使用命令`pip install beautifulsoup4`和`pip install requests`来进行安装。 2. 在你的Python代码中,导入所需的库: ```python import requests from bs4 import BeautifulSoup ``` 3. 设置请求头,以免被京东发现是爬虫。你可以使用以下请求头: ```python headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE'} ``` 4. 构造要爬取商品页面的URL。比如,如果你要爬取iPhone的价格,你可以使用以下URL: ```python url = "https://search.jd.com/Search?keyword=iPhone" ``` 请注意替换`iPhone`为你要爬取商品名称。 5. 发送GET请求获取京东商品页面的源码: ```python response = requests.get(url, headers=headers) html = response.text ``` 6. 使用BeautifulSoup解析源码,并提取商品价格。通过查看页面的HTML结构,找到包含商品价格的元素的CSS选择器,然后使用BeautifulSoup进行解析: ```python soup = BeautifulSoup(html, 'html.parser') price = soup.select('.p-price') # 使用正确的CSS选择器,请根据实际情况进行更改 ``` 请注意,这只是一个示例,你需要根据京东商品页面的实际HTML结构来确定正确的CSS选择器。 7. 最后,你可以将爬取到的商品价格进行处理和保存。可以将其打印出来,或者将其保存到文件或数据库中,以供后续使用。 请注意,京东网站可能会对爬虫进行限制,请确保你的爬虫行为符合网站的使用规则,并添加适当的延时和异常处理机制,以避免被封IP或其他问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [入门级,超简单的python使用requests+bs4库实现京东商品获取(附代码)](https://blog.csdn.net/qq_41738750/article/details/120052993)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python爬虫 批量采集京东商品数据,实时了解商品价格走势](https://blog.csdn.net/ooowwq/article/details/122435249)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值