pycharm设置python3_python3之开发环境PyCharm配置

1. 安装PyCharm(安装时注意选择python),地址: https://www.jetbrains.com/pycharm/

3. 打开PyCharm 菜单:File->Settings->Project:x->Project Interpreter

4.以爬虫为例,在上图搜索BeautifulSoup并安装(python3请安装BeautifulSoup4)

实例源码如下,内容来自:https://blog.csdn.net/csdn2497242041/article/details/77170746

from urllib import request

from bs4 import BeautifulSoup # Beautiful Soup是一个可以从HTML或XML文件中提取结构化数据的Python库

# 构造头文件,模拟浏览器访问

url = "http://www.jianshu.com"

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}

page = request.Request(url, headers=headers)

page_info = request.urlopen(page).read().decode('utf-8') # 打开Url,获取HttpResponse返回对象并读取其ResposneBody

# 将获取到的内容转换成BeautifulSoup格式,并将html.parser作为解析器

soup = BeautifulSoup(page_info, 'html.parser')

# 以格式化的形式打印html

print(soup.prettify())

titles = soup.find_all('a', 'title') # 查找所有a标签中class='title'的语句

'''

# 打印查找到的每一个a标签的string和文章链接

for title in titles:

print(title.string)

print("http://www.jianshu.com" + title.get('href'))

'''

# open()是读写文件的函数,with语句会自动close()已打开文件

with open(r"F:\Python\articles.txt","w",encoding='utf-8') as file: # 在磁盘以只写的方式打开/创建一个名为 articles 的txt文件

for title in titles:

file.write(title.string + '\n')

file.write("http://www.jianshu.com" + title.get('href') + '\n\n')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值