【发布时间】:2019-11-24 01:56:24
【问题描述】:
我正在尝试从旧网站获取标题。
在某些情况下我遇到的问题 - null 值。
因此,我尝试做一个while循环并更改URL。
我的While 循环在正确的位置吗?
过程是这样的:
- 打开文件
- 获取网址
- 查看网址
- 获取标题
- 打印标题
- 而标题 = null):
- 替换部分url,再次检查url
from urllib.request import urlopen
from bs4 import BeautifulSoup
from openpyxl import Workbook
import os
import xlrd
import lxml
# set file location
os.chdir("/excel_files")
# set the name of the file
file_name = "old.xlsx"
# open workbook
workbook = xlrd.open_workbook(file_name)
# set existing worksheet
sheet = workbook.sheet_by_index(0)
temp_list = [20131022212405,20090127003537,2009012702352,]
for i in range(sheet.nrows):
try:
u = sheet.cell_value(i,1)
html = urlopen(u)
bsObj = BeautifulSoup(html.read(), features='lxml')
# get title
title = str(bsObj.title)
print('row no. ',i, 'title is :' , title)
except:
title = 'null'
while (title == 'null'):
try:
u = u.replace(temp_list[i], temp_list[i + 1])
html = urlopen(u)
bsObj = BeautifulSoup(html.read(), features='lxml')
title = str(bsObj.title)
except:
print('title is :',title)
我一直得到null - 而不是只得到实际上是null 的行。
【问题讨论】:
标签: python python-3.x arraylist while-loop beautifulsoup