Python Crawling Questions

Asked 2 years ago, Updated 2 years ago, 75 views

I want to crawl the yellow part of the picture.

I parsed up to HTML, but... I don't know how to print out the name and value

import time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup


option_lists = driver.find_elements_by_css_selector('#contractInfo > tbody > tr:nth-child(2) > td:nth-child(3)')
driver.implicitly_wait(1)
html = driver.page_source
soup = BeautifulSoup(html,'html.parser')

python crawling

2022-09-22 18:04

1 Answers

I don't know the address and I don't have html contents to try the yellow part of the picture.

Please refer to the attached sample below.

html = '''
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" version="XHTML+RDFa 1.0" dir="ltr" class="js">
<body>
<div id="aaaa_0">
<input type="checkbox" name="custom_name" value="11111">
<input type="checkbox" name="custom_name2" value="11111">
</div>
<div id="aaaa_1">bbbb</div>
<div id="bbbb_0">bbbb</div>
</body>
</html>
'''


import re
import bs4

bs = bs4.BeautifulSoup(html, 'html.parser')

bs.find ('input', attrs={'name':'custom_name'}) # Find only one

For tag in bs.find_all('input'): # option input may be multiple.
    print(f"name={tag.attrs['name']} value={tag.attrs['value']}")

name=custom_name value=11111
name=custom_name2 value=11111


2022-09-22 18:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.