记一次爬虫项目-杭州19楼

这个项目我爬的是19楼中我要爆料模块,具体代码如下:

import requests
import re
import time
import random
import threading
import pymysql
from lxml import etree
from bs4 import BeautifulSoup

def lou_spider(key=None):
    url_str = 'https://www.19lou.com/forum-269-{}.html'
    def child_spider(i):
        url_ = url_str.format(i)
        # time.sleep(random.uniform(2, 3))
        result = requests.get(url_)
        html = etree.HTML(result.text)
        divs = html.xpath("//div[@class='list-data  ']//div[@class='list-data-item second-data']")
        for child in divs:
            try:
                title = child.xpath(".//div[@class='title']//a//span/text()")[0]
                url1 = child.xpath(".//div[@class='title']//a/@href")[0]
                author = child.xpath(".//div[@class='author']/a//text()")[0]
          
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
感谢您的提问。我理解您想了解如何用 Python 爬虫来爬取雪球网的金融数据。下面是一个简单的爬虫项目,可以帮助您实现这个目标: 1. 确定爬取的目标网页 我们需要确定要爬取的目标网页。在本例中,我们将使用雪球网的金融数据页面:https://xueqiu.com/hq。 2. 使用 requests 库发送 HTTP 请求 我们可以使用 Python 的 requests 库来发送 HTTP 请求并从目标网页获取数据。以下是如何发送 GET 请求的示例代码: ```python import requests url = 'https://xueqiu.com/hq' response = requests.get(url) print(response.text) ``` 3. 解析 HTML 页面 我们可以使用 Python 的 Beautiful Soup 库来解析 HTML 页面。以下是如何使用 Beautiful Soup 库解析响应的 HTML 页面的示例代码: ```python from bs4 import BeautifulSoup soup = BeautifulSoup(response.text, 'html.parser') # 打印解析后的 HTML 页面 print(soup.prettify()) ``` 4. 提取数据 一旦我们成功解析了 HTML 页面,就可以使用 Beautiful Soup 库中的一些方法来提取我们需要的数据。以下是如何从 HTML 页面中提取股票代码和股票名称的示例代码: ```python # 找到包含股票代码和名称的表格 table = soup.find('table', {'class': 'table'}) # 找到所有的行 rows = table.find_all('tr') # 遍历每一行,并提取股票代码和名称 for row in rows: cols = row.find_all('td') if len(cols) > 0: code = cols[0].text.strip() name = cols[1].text.strip() print(code, name) ``` 5. 存储数据 最后,我们可以将提取的数据存储到文件中。以下是如何将股票代码和名称存储到 CSV 文件中的示例代码: ```python import csv with open('stocks.csv', 'w', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(['Code', 'Name']) for row in rows: cols = row.find_all('td') if len(cols) > 0: code = cols[0].text.strip() name = cols[1].text.strip() writer.writerow([code, name]) ``` 希望这个爬虫项目可以帮助您了解如何用 Python 爬虫来爬取雪球网的金融数据。如果您还有其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值