Blog Post

编程错误

August 15, 2024
1.问题描述:保存的pdf数目和excel行数对应不上
原因:代码对excel数据的大小写不敏感,'pdf.pdf'和'PDF.pdf'可以对应一个文件

解决方案:读取excel行数据后统一转换成小写再去重

2.如何打印pdf
async def save_webpage_as_pdf(url, pdf_path):
    try:
        async with async_playwright() as p:
            browser = await p.chromium.launch()
            context = await browser.new_context(user_agent=random.choice(user_agents))
            page = await context.new_page()
            await page.goto(url, timeout=60000)
            await page.pdf(path=pdf_path, format='A4')
            await browser.close()
        return True
    except Exception as e:
        logger.error(f"Failed to load {url}: {e}")
        await browser.close()
        return False

解决方案:选择playwright库,py2url存在乱码,其他的(pypeteer)存在不稳定的情况

3.关键词和标题匹配不照应
if keyword in title_lower:
if re.search(r'\b' + re.escape(keyword) + r'\b', title_lower):

应该使用正则表达式精确匹配,而不是采用第一中方法字符串匹配(忽略了单词边界)