2024-08-16

由于上述代码已经涵盖了使用正则表达式、BeautifulSoup 4 (bs4)、XPath 解析网页的详细过程,并且是一个实际的爬虫示例,因此不需要提供额外的代码。但是,为了更好地理解这些技术的应用,我们可以提供一个简化的代码实例,演示如何使用正则表达式和BeautifulSoup解析简单的HTML内容。




import re
from bs4 import BeautifulSoup
 
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<div class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</div>
<p class="story">...</p>
"""
 
soup = BeautifulSoup(html_doc, 'html.parser')
 
# 使用BeautifulSoup查找链接
links = soup.find_all('a')
for link in links:
    print(link.get('href'))
 
# 使用正则表达式提取标题
title_pattern = re.compile(r'<b>(.*?)</b>')
title_match = title_pattern.search(html_doc)
if title_match:
    print(title_match.group(1))

这个例子展示了如何使用BeautifulSoup查找HTML文档中所有的链接,并使用正则表达式提取文档标题。这是爬虫技术在处理简单HTML内容时的一个常见用法。

2024-08-16



import requests
from bs4 import BeautifulSoup
import pandas as pd
 
# 设置请求头,伪装为浏览器访问
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
 
# 目标URL
url = 'https://www.qunar.com/'
 
# 发送GET请求
response = requests.get(url, headers=headers)
 
# 检查请求是否成功
if response.status_code == 200:
    # 使用BeautifulSoup解析HTML内容
    soup = BeautifulSoup(response.text, 'html.parser')
    
    # 提取所需的数据,这里以主要城市为例
    cities = soup.find_all('a', {'class': 'nav-city-name'})
    city_names = [city.text for city in cities]
    city_urls = ['https://www.qunar.com' + city['href'] for city in cities]
    
    # 将数据存储在字典中
    data = {
        'City Name': city_names,
        'City URL': city_urls
    }
    
    # 将数据转换为DataFrame
    df = pd.DataFrame(data)
    
    # 打印前几行数据
    print(df.head())
 
else:
    print("请求失败,状态码:", response.status_code)
 

这段代码使用了requests库来发送HTTP GET请求,使用BeautifulSoup库来解析HTML内容,并使用pandas库来处理和存储数据。代码首先设置了请求头,伪装成了浏览器,然后发送请求,如果请求成功,它会解析HTML,提取旅游网站首页上的主要城市名称和对应的URL,并将这些数据存储在DataFrame中,最后打印出前几行数据。

2024-08-16



require 'nokogiri'
require 'open-uri'
 
# 定义一个方法来下载和解析知乎页面
def parse_zhihu_page(url)
  # 使用Nokogiri和open-uri下载页面
  doc = Nokogiri::HTML(open(url))
 
  # 提取问题标题
  question_title = doc.css('head title').text.match(/.*? - Zhihu/) { |m| m[0].delete(' - Zhihu') }
 
  # 提取问题详情
  question_details = doc.css('.QuestionHeader-detail').text
 
  # 提取所有高质量回答的用户名
  top_answers = doc.css('.AnswerItem').map do |answer|
    author = answer.css('.AnswerItem-authorName a').text
    { author: author }
  end
 
  # 返回一个包含所有信息的哈希
  {
    title: question_title,
    details: question_details,
    top_answers: top_answers
  }
end
 
# 使用方法
url = 'https://www.zhihu.com/question/29134179'
parsed_data = parse_zhihu_page(url)
puts parsed_data

这段代码使用了Nokogiri库来解析HTML,并使用OpenURI来下载知乎页面。代码提取了问题标题、详情,并遍历高质量回答列表来提取作者名字。最后,它返回了一个包含所有提取信息的哈希。这个例子展示了如何使用Ruby进行基本的网页爬取和数据解析。

2024-08-16



import requests
from bs4 import BeautifulSoup
import pandas as pd
 
# 定义一个函数来获取页面的 HTML 内容
def get_html(url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
        response = requests.get(url, headers=headers)
        return response.text
    except requests.exceptions.RequestException as e:
        print(f"An error occurred: {e}")
        return None
 
# 定义一个函数来解析 HTML 内容,提取项目信息
def parse_html(html):
    soup = BeautifulSoup(html, 'html.parser')
    projects = soup.find_all('div', class_='col-12 mb-3')
    data = []
    for project in projects:
        title = project.find('h1', class_='lh-condensed').text.strip()
        description = project.find('p', class_='col-9 fw-bold mb-1').text.strip()
        language = project.find('span', class_='d-inline-flex flex-wrap align-items-center fw-bold').text.strip()
        stars = project.find('a', class_='m-0 text-bold').text.strip()
        data.append({
            'title': title,
            'description': description,
            'language': language,
            'stars': stars
        })
    return data
 
# 定义一个函数来将项目信息写入 CSV 文件
def write_to_csv(data, filename):
    df = pd.DataFrame(data)
    df.to_csv(filename, index=False)
 
# 定义要爬取的 GitHub 主页和文件名
github_url = 'https://github.com/trending'
csv_filename = 'github_trending.csv'
 
# 获取 HTML 内容
html = get_html(github_url)
 
# 解析 HTML 并提取项目信息
projects_data = parse_html(html)
 
# 将项目信息写入 CSV 文件
write_to_csv(projects_data, csv_filename)
 
print(f"{len(projects_data)} projects have been saved to {csv_filename}.")

这段代码首先定义了一个函数get_html来获取给定 URL 的 HTML 内容,然后定义了一个函数parse_html来解析 HTML 并提取项目信息,最后将提取的数据写入CSV文件中。这个过程展示了如何使用Python网络爬虫技术来抓取和处理网页数据的基本步骤。

2024-08-16

由于原始代码已经非常简洁,并且是基于Python爬虫的典型示例,我们可以保持原样,并提供一些可能的改进建议。

  1. 使用更现代的HTTP库:urllib 是一个较旧的库,现代爬虫更倾向于使用 requests 库。
  2. 异常处理:当网页无法访问时,应该进行适当的异常处理。
  3. 使用更合适的解析器:html.parser 是一个简单的HTML解析器,对于复杂的页面可能不够用。考虑使用 BeautifulSouplxml
  4. 使用异步:如果想要提高效率,可以考虑使用异步爬虫。

以下是改进后的代码示例:




import requests
from bs4 import BeautifulSoup
 
def get_page_content(url):
    try:
        response = requests.get(url)
        if response.status_code == 200:
            return response.text
    except requests.exceptions.RequestException:
        return "Error: Page not reachable"
 
def parse_page(html_content):
    soup = BeautifulSoup(html_content, 'html.parser')
    return soup.title.string
 
def main():
    url = "http://example.webscraping.com"
    html_content = get_page_content(url)
    print(parse_page(html_content))
 
if __name__ == "__main__":
    main()

这段代码使用了 requests 库来替换 urllib,使用了 BeautifulSoup 来替换 html.parser,并增加了异常处理。

2024-08-16

urllib和requests都是Python中用于发送HTTP请求的库。

  1. 背景:
  • urllib是Python自带的HTTP请求库,包含了几个模块,提供了各种功能,比如:urllib.request 用于打开和读取URLs,urllib.error 包含了由urllib.request抛出的异常,urllib.parse 用于解析URLs,urllib.robotparse 用于解析robots.txt文件。
  • requests库是一个更简洁、更易用的HTTP请求库,它比urllib更为Pythonic,提供了更高级的功能,比如自动处理cookies和session,多种类型的HTTP请求方法,JSON/XML解析,客户端证书,链接池等。
  1. 定义:
  • urllib是Python内置的HTTP请求库,用于处理URLs,包括网络爬虫,网络数据抓取等。
  • requests是一个第三方库,需要单独安装,它更简洁易用,功能强大,能够进行网络请求,也可以用于网络爬虫,网络数据抓取。
  1. 特点:
  • urllib:

    • 是Python内置的HTTP请求库,不需要单独安装。
    • 提供了大量的HTTP请求功能,包括:URL处理,打开和读取URLs,错误处理等。
    • 使用起来较为复杂,需要自己处理大部分的细节。
  • requests:

    • 是第三方库,需要单独安装(pip install requests)。
    • 提供了简洁易用的API,可以轻易地发送HTTP请求。
    • 支持多种HTTP请求类型,如GET,POST,PUT,DELETE等。
    • 自动处理URL编码和cookies。
  1. 功能:
  • urllib提供的功能:

    • urlopen:打开一个URL
    • urlretrieve:下载URL内容到本地
    • urlcleanup:清除URL缓存
    • quote:URL编码
    • unquote:URL解码
    • urlencode:将字典编码为URL参数
  • requests提供的功能:

    • 发送GET请求
    • 发送HEAD请求
    • 发送POST请求
    • 发送PUT请求
    • 发送DELETE请求
    • 发送PATCH请求
    • 发送OPTIONS请求
    • 连接池管理
    • Cookie持久化
    • 会话维持
    • 文件上传
    • 自动处理重定向
    • 处理认证
    • JSON/XML解析
    • 客户端证书
    • 超时
    • 错误处理
    • 响应状态码处理
    • 打印出详细的请求信息
  1. 代码示例:
  • urllib示例:



import urllib.request
response = urllib.request.urlopen('http://www.example.com/')
html = response.read()
  • requests示例:



import requests
response = requests.get('http://www.example.com/')
html = response.text

在实际应用中,如果需要发送简单的HTTP请求,推荐使用requests库,因为它更简洁易用。如果需要处理复杂的HTTP请求,比如需要处理cookies,session,连接池等,推荐使用urllib库,因为它提供了更多的功能和细节控制。

2024-08-16



import requests
from lxml import etree
import csv
import time
 
# 设置请求头,模拟浏览器访问
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
 
def get_data(url):
    # 发送请求,获取响应
    response = requests.get(url, headers=headers)
    return response.text
 
def parse_data(html):
    # 使用Xpath解析网页
    html = etree.HTML(html)
    # 影片名称
    name = html.xpath('//div[@class="info"]/div[@class="hd"]/a/span[1]/text()')
    # 影评分数
    score = html.xpath('//div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[@class="rating_num"]/text()')
    # 评分人数
    people = html.xpath('//div[@class="info"]/div[@class="bd"]/div[@class="star"]/span[4]/text()')
    # 上映日期
    time = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
    # 导演
    director = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
    # 编剧
    writer = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
    # 类型
    type = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
    # 区域
    area = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
    # 语言
    language = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
    # 上映时间
    time_show = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
    # 集数
    part = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
    # 国家
    country = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
    # 简介
    introduce = html.xpath('//div[@class="info"]/div[@class="bd"]/p[@class=""]/text()')
 
    return name, score, people, time, director, writer, type, area, language, time_show, part, country, introduce
 
def save_data(data):
    # 保存数据到CSV文件
    with open('douban_top250.csv', 'a', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(data)
2024-08-16



import urllib.request
import os
 
# 下载网页内容
def download_page(url):
    with urllib.request.urlopen(url) as response, open('page.html', 'wb') as file:
        file.write(response.read())
 
# 下载图片
def download_image(url, filename):
    with urllib.request.urlopen(url) as response, open(filename, 'wb') as file:
        file.write(response.read())
 
# 下载视频
def download_video(url, filename):
    with urllib.request.urlopen(url) as response, open(filename, 'wb') as file:
        file.write(response.read())
 
# 示例使用
url = 'http://example.com'
download_page(url)  # 下载网页
 
image_url = 'http://example.com/image.jpg'
download_image(image_url, 'image.jpg')  # 下载图片
 
video_url = 'http://example.com/video.mp4'
download_video(video_url, 'video.mp4')  # 下载视频

这段代码提供了三个函数,分别用于下载网页、图片和视频。每个函数都使用了urllib.request来打开网络资源,并将内容写入本地文件。使用时,只需要提供相应的URL和文件名即可。这是一个简单的网络爬虫示例,适合作为初学者理解和实践的基础。

2024-08-16



import requests
import json
 
# 定义要发送的 JSON 数据
data = {
    "key1": "value1",
    "key2": "value2"
}
 
# 将字典转换为 JSON 格式的字符串
json_data = json.dumps(data)
 
# 发送 POST 请求
response = requests.post('http://httpbin.org/post', data=json_data)
 
# 打印响应内容
print(response.text)

确保你已经安装了 requests 库,如果没有安装,可以使用以下命令安装:




pip install requests
2024-08-16

函数的嵌套指的是在一个函数内定义另一个函数。这样做可以隐藏实现细节,并允许你创建独特的、可重用的代码单元。

下面是一个Python函数嵌套的例子:




def outer_function(x):
    # 内部函数
    def inner_function(y):
        return x * y
    
    # 返回内部函数,供外部访问
    return inner_function
 
# 创建一个外部函数的调用
outer = outer_function(10)
 
# 使用内部函数
result = outer(5)  # 结果是 10 * 5 = 50
print(result)

在这个例子中,outer_function 是外部函数,它接受一个参数 x。在 outer_function 的内部,我们定义了一个名为 inner_function 的内部函数,它接受一个参数 y。当我们调用 outer_function 时,实际上返回的是 inner_function,它被赋予了一个参数 x。当我们调用返回的 inner_function(即 outer(5))时,它将使用 x 的值乘以它自己的参数 y