左手python右手stata(一):Billboard No.1 Country Hits 爬虫
“ 怕什么真理无穷,进一寸有一寸的欢喜。” —— 胡适
项目描述:用python爬取并解析网页数据,再用stata处理
项目成果:1944-2020 Billboard 乡村音乐热榜
资源自取:https://share.weiyun.com/5yI6Z6e 密码:p9rvuj
欢迎关注个人公号:dm2fyq,分享经济/计量/软件/前沿学习心得
目标和结果
代码
1.1 python爬取网址
# 以下代码Python运行
import requests # 引入request包
def read_pageHtml(url): # 定义函数
file = request.urlopen(url)
data = file.read() # 将url内容读入data变量
return data
url = "https://en.wikipedia.org/wiki/List_of_Billboard_number-one_country_hits" # 需科学上网,可自定义url
data = read_pageHtml(url) # 调用定义的函数
with open(r"C:\……\test.txt","wb