python爬虫(vscode版,借鉴于知乎用户:干饭小熊猫,仅用于自用)

一、先下载requests和beautifulsoup4两个库(我是在vscode的终端用pip下载的)

eb2e60a5397c4241ac1a4e7d5f146ede.png

 

二、获取爬虫的header和cookie

①打开所要爬的网页,点击鼠标右键,然后点击检查e4366645274a48cca12fd241d0c9b1cc.png

 ②点击网络选项

0b39849235814ae2817ace072f461550.png

③按下ctrl+R刷新界面,在名称中找到想要爬取的文件 (一般为第一个)复制为cURL7d3bf79fa6304f458968517b15f864c4.png

 ④然后打开网页Convert curl commands to code,将刚刚复制的内容输入,得到代码

79e8a9d2f3944ca0991d51e53c7256e3.png

 ⑤复制进vscode(注意response的网址不要按他给的,自己复制网址放上去)

5374a6f083b04901a72f573709fc7dae.png

这里获取的网页没有cookie,有些是有的,如下

ff407b0168e64b53b4b1e768d4815cb8.png

三、点开“元素”选项,,点击左上角的箭头方框,然后点击你想要爬的部分网页,在右边找到你想要爬取的那部分内容的代码,复制为selector,然后放入vscode代码的content的变量中,content="#j_isend > div"

426aaae0d6444c88acf2070f899f7846.png

 cdc940ade7c54d0cb1bee3726151c850.png

四、创建一个文件,存储爬了的内容

fo = open("C:\\Users\\ASUS\\Desktop\\python有关文件\\教你如何爬取网页.txt",'w',encoding="utf-8")
response.encoding='utf-8'

五、清洗数据(soup有网页抓取数据的作用,使拿到的数据为有效数据,而不是编码什么的)

soup = BeautifulSoup(response.text, 'html.parser')
a=soup.select(content)
for i in range(0,len(a)):
    a[i] = a[i].text
    fo.write(a[i]+'\n')
fo.close()

 总代码如下:

import os
import requests
from bs4 import BeautifulSoup
import requests

headers = {
    'sec-ch-ua': '"Chromium";v="112", "Microsoft Edge";v="112", "Not:A-Brand";v="99"',
    'Referer': 'https://www.docin.com/p-2105164690.html',
    'sec-ch-ua-mobile': '?0',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.58',
    'sec-ch-ua-platform': '"Windows"',
}

params = {
    'rand': '20230427',
}

response = requests.get('https://www.docin.com/p-2105164690.html', params=params, headers=headers)
content="#j_isend > div"
#数据存储
fo = open("C:\\Users\\ASUS\\Desktop\\python有关文件\\教你如何爬取网页.txt",'w',encoding="utf-8")
response.encoding='utf-8'
soup = BeautifulSoup(response.text, 'html.parser')

#清洗数据
a=soup.select(content)
for i in range(0,len(a)):
    a[i] = a[i].text
    fo.write(a[i]+'\n')
fo.close()

  • 22
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
可以使用 Python 的 Requests 和 BeautifulSoup 库来爬取用户信息。首先需要登录乎获取 cookie,然后通过模拟登录获取到用户的个人主页,再使用 BeautifulSoup 解析页面获取用户信息。 以下是示例代码: ```python import requests from bs4 import BeautifulSoup # 登录乎并获取 cookie session = requests.Session() 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'} login_url = 'https://www.zhihu.com/signin' response = session.get(login_url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') _xsrf = soup.find('input', attrs={'name': '_xsrf'})['value'] captcha_url = soup.find('img', attrs={'class': 'Captcha-englishImg'})['src'] # 模拟登录获取用户信息 login_data = { '_xsrf': _xsrf, 'email': 'your_account', 'password': 'your_password', 'captcha': input('请输入验证码' + captcha_url), 'remember_me': 'true' } session.post(login_url, headers=headers, data=login_data) user_url = 'https://www.zhihu.com/people/username' response = session.get(user_url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') # 解析页面获取用户信息 name = soup.find('span', attrs={'class': 'ProfileHeader-name'}).text headline = soup.find('span', attrs={'class': 'RichText ztext ProfileHeader-headline'}).text description = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-description'}).find('span', attrs={'class': 'RichText ztext'}).text.strip() location = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-location'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() business = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-business'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() employment = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-employment'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() position = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-position'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() education = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-education'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() major = soup.find('div', attrs={'class': 'ProfileHeader-infoItem ProfileHeader-major'}).find('span', attrs={'class': 'ProfileHeader-detailValue'}).text.strip() ``` 以上代码中,需要替换 `your_account` 和 `your_password` 为你的乎登录账号和密码,并将 `username` 替换为你要爬取的用户用户名。另外,为了防止被乎反爬虫机制检测到,最好加上一些随机的等待时间和 User-Agent 等信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值