简体   繁体   中英

Python 2.7/Selenium 2 AttributeError: 'NoneType' object has no attribute 'group'

I am fairly new to Python/Selenium and have been ceaselessly trying to resolve an issue with some code I'd like to modify with no luck. After researching on the internet and trying various changes for days, I'm at my wits end and was hoping someone here could help. I apologize in advance if any of the terminology I use is misguided or incorrect. If you need any additional information, please let me know.

Specifically, I would like to run a script to automate torrent downloading / transcoding / re-uploading. One issue in particular that I'm having trouble with is as follows:

The script is written primarily in Python and also relies on elements of Selenium and Transmission daemon/remote to function. Once it has navigated to the website and chosen a torrent to download, it pulls information from the page using xpaths. I'm not sure if it's this part of the code or the following that is causing it to break, but when there is no release date and / or additional info listed the script stops running and returns an error.


Traceback (most recent call last):
  File "main.py", line 30, in <module>
    flac, missing, date, media_type, ul_page, cat_num, rel_type, seeders = get_torrent.get_available(driver)
  File "/home//Desktop/get_torrent.py", line 38, in get_available
    release_date = re.search('\d{4}-\d{2}-\d{2}', additional_info).group(0)
AttributeError: 'NoneType' object has no attribute 'group'

From what I can tell, either:

  • The website doesn't have any info to be stored in additional_info so it is being set to None which doesn't have a group 0 attribute, or...

  • the additional_info does not contain the re.search query which is causing the error.

Either way, what I'd like to do is something like this. if (whatever is causing the error; ie additional_info is blank, re.search reference doesn't exist, etc.) is true, then skip trying to store re.search('\\d...) to release_date and instead restart the torrent search process to find one that doesn't have that issue. (You can't re-upload a torrent that doesn't have a release date so those aren't viable options).

Code snippet in question:


additional_info = driver.find_element_by_xpath("//tr[@class='edition_info'][last()]/td").text
release_date = re.search('\d{4}-\d{2}-\d{2}', additional_info).group(0)
catalog_num = re.search('[A-Z]+-[0-9]+', additional_info.split('/')[-2:-1][0])

The common pattern to handle situations when there is no match is:

additional_info = driver.find_element_by_xpath("//tr[@class='edition_info'][last()]/td").text 
match = re.search(r'\d{4}-\d{2}-\d{2}', additional_info)
if match:
    release_date = match.group(0)

Or, you may set the release_date to a default value, None for instance:

release_date = match.group(0) if match else None

Also, here is a relevant discussion:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM