|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Tihool 于 2022-5-1 15:29 编辑
import xlwt
import urllib.request
from bs4 import BeautifulSoup
def main():
url ='https://news.house.qq.com/a/20170702/003985.htm'
res = open_url(url)
data = get_data(res)
datas = save_file(data)
def open_url(url):
head = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
}
res = urllib.request.Request(url,headers=head)
response = urllib.request.urlopen(res)
print(response)
return response
def get_data(url):
data = []
b = re.compile(r'\[(.+)\]')
soup = BeautifulSoup(url,"html.parser")
target = soup.find_all('p', style="TEXT-INDENT: 2em",class_="text")
target = iter(target)
for each in target:
if each.get_text().isnumeric():
data.append([
re.findall(next(target).text,b),,#此处如何修改
next(target).text,
next(target).text,
next(target).text])
print(data)
return data
def save_file(data):
name = ['城市',"房价",'工资','工资比']
workbook = xlwt.Workbook(encoding = 'utf-8')
worksheet = workbook.add_sheet('sheet1')
for i in range(4):
worksheet.write(0,i,name[i])
for i,_ in enumerate(data):
Data = data[i]
for j,v in enumerate(Data):
worksheet.write(i+1,j,v)
workbook.save('房价.xls')
if __name__ == "__main__":
main()
|
|