【python】网络爬虫_python网络爬虫软件

liftword2个月前 (02-21)技术文章15

1. 使用requests获取网页

获取网页内容:

import requests
url = 'https://example.com'
response = requests.get(url)
html = response.text

2. 使用BeautifulSoup解析 HTML

解析 HTML 并提取数据:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify())  # Pretty-print the HTML

3. 遍历 HTML 树

使用标签查找元素:

title = soup.title.text  # Get the page title
headings = soup.find_all('h1')  # List of all 

tags

4. 使用 CSS 选择器

使用 CSS 选择器选择元素:

articles = soup.select('div.article')  # All elements with class 'article' inside a 

5. 从标签中提取数据

从 HTML 元素中提取文本和属性:

for article in articles:
    title = article.h2.text  # Text inside the 

tag link = article.a['href'] # 'href' attribute of the tag print(title, link)

6. 处理相对 URL

将相对 URL 转换为绝对 URL:

from urllib.parse import urljoin
absolute_urls = [urljoin(url, link) for link in relative_urls]

7. 处理分页

爬取多个页面上的内容:

base_url = "https://example.com/page/"
for page in range(1, 6):  # For 5 pages
    page_url = base_url + str(page)
    response = requests.get(page_url)
    # Process each page's content

8. 处理 AJAX 请求

爬取由 AJAX 请求加载的数据:

# Find the URL of the AJAX request (using browser's developer tools) and fetch it
ajax_url = 'https://example.com/ajax_endpoint'
data = requests.get(ajax_url).json()  # Assuming the response is JSON

9. 在网络爬取中使用正则表达式

使用正则表达式提取数据:

import re
emails = re.findall(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', html)

10. 尊重robots.txt

要检查robots.txt以获取抓取权限:

from urllib.robotparser import RobotFileParser
rp = RobotFileParser()
rp.set_url('https://example.com/robots.txt')
rp.read()
can_scrape = rp.can_fetch('*', url)

11. 使用会话和 Cookie

为了维护会话和处理 Cookies:

session = requests.Session()
session.get('https://example.com/login')
session.cookies.set('key', 'value')  # Set cookies, if needed
response = session.get('https://example.com/protected_page')

12. 使用浏览器自动化进行抓取(selenium库)

爬取由 JavaScript 渲染的动态内容:

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://example.com')
content = browser.page_source
# Parse and extract data using BeautifulSoup, etc.
browser.quit()

13. 网络爬取中的错误处理

处理错误和异常:

try:
    response = requests.get(url, timeout=5)
    response.raise_for_status()  # Raises an error for bad status codes
except requests.exceptions.RequestException as e:
    print(f"Error: {e}")

14. 异步网页抓取

异步抓取网站以实现更快的数据检索:

import aiohttp
import asyncio
async def fetch(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            return await response.text()urls = ['https://example.com/page1', 'https://example.com/page2']
loop = asyncio.get_event_loop()
pages = loop.run_until_complete(asyncio.gather(*(fetch(url) for url in urls)))

15. 数据存储(CSV,数据库)

将抓取的数据存储到 CSV 文件或数据库中:

import csv
with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['Title', 'URL'])
    for article in articles:
        writer.writerow([article['title'], article['url']])

相关文章

Python实现一个基础爬虫?_用python做一个爬虫

Python爬虫技术就是指通过Python语言来编写一些自动化的数据处理程序从网页上来获取自己想要的数据,我们可以通过Python爬虫来获取公开网页上的数据,对数据进行分析、存储、数据可视化展示等操作...

什么是Python爬虫?一篇文章带你全面了解爬虫

一、什么叫爬虫爬虫,又名“网络爬虫”,就是能够自动访问互联网并将网站内容下载下来的程序。它也是搜索引擎的基础,像百度和GOOGLE都是凭借强大的网络爬虫,来检索海量的互联网信息的然后存储到云端,为网友...

程序员python入门课,30分钟学会,30行代码写爬虫项目

现在很多人学习编程,最开始就是选择的python,因为python现在比较火,薪资水平在程序员领域也是比较高的,入门快,今天就给大家分享一个用python写的小爬虫项目,只需要30行代码,认真学习,...

巨细!Python爬虫详解_python爬虫详细教程

来源:【公众号】Python技术爬虫(又称为网页蜘蛛,网络机器人,在 FOAF 社区中间,更经常的称为网页追逐者);它是一种按照一定的规则,自动地抓取网络信息的程序或者脚本。如果我们把互联网比作一张大...

Python爬虫常用的8个技巧,让你爬取数据得心应手

今天跟大家分享几个我在爬虫中用到的技巧,让你轻松爬取所需数据。技巧一:随机暂停,迷惑反爬机制高频率访问容易被网站识别为爬虫,所以我们要学会“劳逸结合”!使用 time.sleep() 函数,加上随机时...

超详细的python爬虫案例,一次爬取上百篇文章

一次爬多篇文章,逻辑是先从主网址爬到每篇文章的网址,再从各个网址处爬取文章,很简单的静态网页爬取,不需考虑反扒问题。话不多说,直接案例说话。实例:从https://www.biquge.com.cn/...