一、Requests安装及示例
爬虫爬取网页内容首先要获取网页的内容,通过requests库进行获取。
-
GitHub: https://github.com/requests/requests
-
PyPl: https://pypi.python.org/pypi/requests
-
官方文档:http://wwwpython-requests.org
-
中文文档:http://docs.python-requests.org/zh CN/latest
安装
pip install requests
示例代码
import requests`` ``url = "http://store.weigou365.cn"``res = requests.get(url)``res.text
执行效果如下:
二、Selenium库
爬虫爬取网页有时需要模拟网页行为,比如京东、淘宝详情页面,图片加载随着滚动自动加载的。这种情况我们就要进行浏览器模拟操作才能获取要爬取的数据。
Selenium 是一个用于自动化浏览器操作的开源框架,主要用于网页测试,支持多种浏览器包括 Chrome、Firefox、Safari 等。它提供了一系列的API,允许你模拟用户在浏览器中的行为,例如点击按钮、填写表单、导航等。
官方网站: https://sites.google.com/a/chromium.org/chromedriver``114之前版本:http://chromedriver.storage.googleapis.com/index.html``116版本:https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/win64/chromedriver-win64.zip``117之后的版本:https://googlechromelabs.github.io/chrome-for-testing/
安装
pip install selenium
示例代码
`from selenium import webdriver``browser = webdriver.Chrome()``browser.get("https://baidu.com/")``print(browser.title)``browser.quit()`
三、爬取京东详情页面代码****
`from selenium import webdriver``from lxml import etree``import time``import openpyxl``import re``import os``import reque