这个项目我爬的是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]
time_ = child.xpath(".//div[@class='author']/span/text()")[0]
read_num = child.xpath(".//div[@class='num numeral']//span[@class='num-read']/text()")[0]

本文记录了一次使用Python爬虫抓取杭州19楼我要爆料模块的经历。在爬取过程中,首先遇到请求内容获取不到的问题,尝试采用selenium但因在CentOS服务器上安装环境困难而放弃,最终通过调整requests请求头成功解决问题。同时,为避免IP被封,采用了设置延迟(sleep)和多进程策略,但仍在寻找更有效的解决方案,如使用代理。
最低0.47元/天 解锁文章
6万+

被折叠的 条评论
为什么被折叠?



