python的for循环问题

下面是代码区域


import requests
import time
import re
import openpyxl



wb = openpyxl.load_workbook('每天.xlsx')
ws = wb['Sheet']

with open("lj.txt")as f:
 links = f.readlines()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'}
wenjian = []

for link in links:
 response = requests.get(link, headers=headers)
 response.encoding = 'gbk'
 html_str = response.text
 time.sleep(1)
 wenjian.append(html_str)


wj = wenjian


guimo = re.compile('colspan="10">本场比赛必发交易(.*?)', re.S)
qingxiang = re.compile('colspan="10">本场比赛必发成交量(.*?) class="ying">(.*?),(.*?) class="ying">(.*?)', re.S)
sheng = re.compile('class="data-detail".*?
  • (.*?)
  • ',
    re.S) ping = re.compile('class="data-detail".*?li.*?
  • (.*?)
  • ',
    re.S) fu = re.compile('class="data-detail".*?
  • .*?
  • .*?
  • (.*?)
  • ',
    re.S) gm = re.findall(guimo, wj) qx = re.findall(qingxiang, wj) s = re.findall(sheng, wj) p = re.findall(ping, wj) f = re.findall(fu, wj) for i, link in enumerate(gm): ws.cell(row=i + 2, column=9).value = link for i, link in enumerate(qx): ws.cell(row=i + 2, column=10).value = link for i, link in enumerate(s): ws.cell(row=i + 2, column=11).value = link for i, link in enumerate(p): ws.cell(row=i + 2, column=12).value = link for i, link in enumerate(f): ws.cell(row=i + 2, column=13).value = link #这行还未完善,删除多余的周几 # for i, link in enumerate(bh): # if ws.cell(row=i+2, column=23).value==2: # # ws.delete_rows(row) # ws.delete_rows wb.save("每天.xlsx")

    最上面的for循环那一块,怎么都不满意,我吧links内的所有连接先爬取然后,获得文本内容,正则表达式获取我想的文字,放进excel里面
    结果啥也没有,然后之前有时候可以获取一条,我想要全部都能下来

    你可以在for循环那里打印出来看看有没有数据,有可能是你的url有问题

    该回答引用ChatGPT
    1、你从文件中读取链接列表 links,但是每个链接后面包含一个换行符,需要将其去除。可以使用 strip() 方法去除字符串开头和结尾的空白字符,例如 link = link.strip()。

    2、在获取网页内容后,你将其放到了 wenjian 列表中,但是后面使用正则表达式进行匹配时直接使用了 wenjian,这可能会导致匹配不到正确的内容。可以将 html_str 直接用于正则表达式的匹配,例如 gm = re.findall(guimo, html_str)。

    3、你的正则表达式可能需要进一步调整,以确保能够匹配到正确的内容。

    4、如果你的链接列表较长,可以考虑使用多线程或异步请求,以加快爬取速度。

    以下是一个修改后的示例代码,你可以参考一下

    
    import requests
    import time
    import re
    import openpyxl
     
    wb = openpyxl.load_workbook('每天.xlsx')
    ws = wb['Sheet']
     
    with open("lj.txt") as f:
        links = [link.strip() for link in f.readlines()]
     
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'}
     
    for i, link in enumerate(links):
        response = requests.get(link, headers=headers)
        response.encoding = 'gbk'
        html_str = response.text
        time.sleep(1)
     
        # 使用正则表达式匹配需要的内容
        guimo = re.compile('<td colspan="10">本场比赛必发交易(.*?)</td>', re.S)
        qingxiang = re.compile('<td colspan="10">本场比赛必发成交量(.*?)<em class="ying">(.*?),(.*?)<em class="ying">(.*?)</td>', re.S)
        sheng = re.compile('class="data-detail".*?<li>胜(.*?)</li>', re.S)
        ping = re.compile('class="data-detail".*?<li>平(.*?)</li>', re.S)
        fu = re.compile('class="data-detail".*?<li>负(.*?)</li>', re.S)
        gm = re.findall(guimo, html_str)
        qx = re.findall(qingxiang, html_str)
        s = re.findall(sheng, html_str)
        p = re.findall(ping, html_str)
        f = re.findall(fu, html_str)
     
        # 将结果写入 Excel 表格
        ws.cell(row=i + 2, column=1).value = link
        for j, text in enumerate(gm):
            ws.cell(row=i + 2, column=9 + j).value = text
        for j, text in enumerate(qx):
            ws.cell(row=i + 2, column=11 + j).value = text
        for j, text in enumerate(s):
            ws.cell(row=i + 2, column=14 + j).value = text
        for j, text in enumerate(p):
            ws.cell(row=i + 2, column=17 + j).value = text
        for j, text in enumerate(f):
            ws.cell(row=i + 2, column=20 + j).value = text
     
    wb.save("每天.xlsx")