我正在尝试打开一个网页并将一些字符串从其中抓取到列表中。该列表最终将由网页上显示的所有名称填充。为了尝试这样做,我的代码如下所示:
import xlsxwriter, urllib.request, string, http.cookiejar, requests
def main():
username = 'john.mauran'
password = 'fZSUME1q'
log_url = 'https://aries.case.com.pl/'
dest_url = 'https://aries.case.com.pl/main_odczyt.php?strona=eksperci'
login_values = {'username' : username , 'password' : password }
r = requests.post(dest_url, data=login_values, verify=False, allow_redirects=False)
open_sesame = r.text
#reads the expert page
readpage_list = open_sesame.splitlines()
#opens up a new file in excel
workbook = xlsxwriter.Workbook('expert_book.xlsx')
#adds worksheet to file
worksheet = workbook.add_worksheet()
#initializing the variable used to move names and dates
#in the excel spreadsheet
boxcoA = ""
boxcoB = ""
#initializing expert attribute variables and lists
url_ticker = 0
name_ticker = 0
raw_list = []
url_list = []
name_list= []
date_list= []
#this loop goes through and finds all the lines
#that contain the expert URL and name and saves them to raw_list::
#raw_list loop
for i in open_sesame:
if '<tr><td align=left><a href=' in i:
raw_list += i
if not raw_list:
print("List is empty")
if raw_list:
print(raw_list)
main()
如您所见,我想做的就是从请求操作返回的文本中获取以以下字符开头的行:
最佳答案
我不知道你到底想做什么,但这没有任何意义:
for i in open_sesame:
if '<tr><td align=left><a href=' in i:
raw_list += i
首先,如果您迭代 open_sesame
,它是一个字符串,迭代中的每个项目将是字符串中的一个字符。然后'<tr><td align=left><a href=' in i
永远都是假的。
其次,raw_list += i
不是将项目附加到列表的方式。
最后,为什么这个变量叫open_sesame
?这是开玩笑吗?
关于python - 如果 [string] 中的 [string] 不适用于请求的网页文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27145082/