from selenium import webdriver
from bs4 import BeautifulSoup
import sys
import xlwt
workbook = xlwt.Workbook(encoding="utf-8")
browers = webdriver.Chrome()
tt = "青岛"
url = "http://www.tianqi.com/"
browers.get(url)
content1 = browers.find_element_by_css_selector("body > div.hhx_newheader > div > div.tit2.right > ul > li > span.hhx_appLoadSpan > a:nth-child(2)")
content1.click()
browers.switch_to.window(browers.window_handles[1])
content2 = browers.find_element_by_css_selector("body > div.header > div.city_t.pageclear > div > table > tbody > tr > td > h9 > a:nth-child(2)")
content2.click()
content3 = browers.find_element_by_link_text(tt)
content3.click()
browers.switch_to.window(browers.window_handles[2])
liebiao = browers.find_elements_by_partial_link_text("月天气")
def jiexi(element):
element.click()
print("打开了一个月份")
html = browers.page_source
soup = BeautifulSoup(html,"lxml")
title = soup.find(class_="box-t-l").get_text()[:-5]
woeksheet = workbook.add_sheet(title)
x = 0
y = 0
for i in soup.find_all(class_="tqtongji2"):
for ii in i.find_all("li"):
xieru(x,y,woeksheet,ii.get_text(0))
browers.back()
def xieru(xx,yy,worksheet,texttt):
if yy <= 5:
worksheet.write(xx,yy,label=texttt)
yy += 1
else:
yy = 0
xx += 1
worksheet.write(xx,yy,label=texttt)
for i in liebiao:
print(liebiao)
jiexi(i)
path = "{}{}{}".format("D:\测试临时",tt,".xls")
workbook.save(path)
Traceback (most recent call last):
File "D:/pyxiangmu/shiyanchang2/青岛天气统计.py", line 44, in <module>
jiexi(i)
File "D:/pyxiangmu/shiyanchang2/青岛天气统计.py", line 30, in jiexi
xieru(x,y,woeksheet,ii.get_text(0))
File "D:\pyxiangmu\venv\lib\site-packages\bs4\element.py", line 955, in get_text
return separator.join([s for s in self._all_strings(
AttributeError: 'int' object has no attribute 'join'